<?php
/**
 * IndexController
 * @author ComsenInc     
 * @copyright  Copyright (c)2007
 * @license
 */
class IndexController extends Zend_Controller_Action {

	function init(){
        $this->db = Zend_Registry::get('db');
        $this->smarty = Zend_Registry::get('smarty');
        $this->cache =Zend_Registry::get('cache');
        Zend_Loader::loadClass('Forums');
        Zend_Loader::loadClass('Threads');
        /* 验证用户是否已经登陆*/
        $this->smarty->assign('user',(array)Zend_Auth::getInstance()->getIdentity());
        $this->smarty->assign('IMGDIR',$this->_request->getBaseUrl().'/'.$this->smarty->template_dir);
 
    }

	function indexAction(){
		 $this->smarty->assign('title','index');
		 /*  查看一个缓存是否存在:*/
         if(!$Forum = $this->cache->load('ForumTreeCache')) {
            $Forums = new Forums();
           /* 返回所有行，到一个数组中 */
           $treecat = $Forums->category_tree();
           /*将数据库返回值缓存到ForumCache这个缓存文件中 */
           $this->smarty->assign('treecat',$treecat);
           $this->cache->save($treecat, 'ForumTreeCache');
         } else {
        	/*缓存命中，直接从缓存中Load出来 */
        	$treecat = $this->cache->load('ForumTreeCache');
        	$this->smarty->assign('treecat',$treecat);
         }
          if(!$Forum = $this->cache->load('Newlist')) {   
            $select = $this->db->select();
            $select->from('cdb_threads', '*');
            $select->join('cdb_forums', 'cdb_threads.fid = cdb_forums.fid', '*');
            $select->join('cdb_memberfields', 'cdb_threads.authorid = cdb_memberfields.uid', '*');
            $select->order('cdb_threads.lastpost');
            $select->limit(20);
            $result = $this->db->fetchAll($select);
       	    $this->cache->save($result, 'Newlist');
            $this->smarty->assign('indextabs',$result);
          }else {
          	$result = $this->cache->load('Newlist');
          	$this->smarty->assign('indextabs',$result);
          }
           
       	   $this->smarty->display('index.tpl');
	    
	}

	
	public function __call($method, $args)    
    {        
        if ('Action' == substr($method, -6)) 
        {            
            // If the action method was not found, forward to the index action
            //如果action方法没有找到,则转向到index action           
            return $this->_forward('index');        
        }        
        // all other methods throw an exception 
        //所有其他的方法则扔出一个错误信息  
        throw new Exception('Invalid method "' . $method . '" called');    
    }
    
    public function noRouterAction()
    {
        $this->_redirect('/');
    }
    
}